using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using ActivitySystem.DataContract;

namespace ActivitySystem.DataAccess
{
    public partial class ActivityTypeDao : IDao<ActivityType>
    {
        public ActivityType Select(Guid id)
        {
            ActivityType activityType = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        activityType = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return activityType;
        }

        public void Insert(ActivityType activityType)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_Insert";
            command.Parameters.Add(new SqlParameter("@Id", activityType.Id));
            command.Parameters.Add(new SqlParameter("@Name", activityType.Name));
            command.Parameters.Add(new SqlParameter("@Description", activityType.Description));
            command.Parameters.Add(new SqlParameter("@IsActive", activityType.IsActive));
            command.Parameters.Add(new SqlParameter("@InsertTimeStamp", activityType.InsertTimeStamp));
            command.Parameters.Add(new SqlParameter("@UpdateTimeStamp", activityType.UpdateTimeStamp));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(ActivityType activityType)
        {
            if (activityType.UpdateTimeStamp == DateTime.MinValue) activityType.UpdateTimeStamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_Update";
            command.Parameters.Add(new SqlParameter("@Id", activityType.Id));
            command.Parameters.Add(new SqlParameter("@Name", activityType.Name));
            command.Parameters.Add(new SqlParameter("@Description", activityType.Description));
            command.Parameters.Add(new SqlParameter("@IsActive", activityType.IsActive));
            command.Parameters.Add(new SqlParameter("@InsertTimeStamp", activityType.InsertTimeStamp));
            command.Parameters.Add(new SqlParameter("@UpdateTimeStamp", activityType.UpdateTimeStamp));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Private Method

        internal static ActivityType CreateEntityByReader(IDataReader reader)
        {
            ActivityType activityType = new ActivityType();
            if (reader["Id"] != DBNull.Value) activityType.Id = (Guid)reader["Id"];
            if (reader["Name"] != DBNull.Value) activityType.Name = reader["Name"] as string;
            if (reader["Description"] != DBNull.Value) activityType.Description = reader["Description"] as string;
            if (reader["IsActive"] != DBNull.Value) activityType.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimeStamp"] != DBNull.Value) activityType.InsertTimeStamp = (DateTime)reader["InsertTimeStamp"];
            if (reader["UpdateTimeStamp"] != DBNull.Value) activityType.UpdateTimeStamp = (DateTime)reader["UpdateTimeStamp"];

            return activityType;
        }

        #endregion
    }
}